package com.ysx.search.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysx.behaviour.feign.ApBehaviorEntryFeign;
import com.ysx.behaviour.pojo.ApBehaviorEntry;
import com.ysx.common.pojo.PageInfo;
import com.ysx.common.pojo.Result;
import com.ysx.common.utils.RequestHeaderInfoUtils;
import com.ysx.core.controller.AbstractCoreController;
import com.ysx.search.dto.SearchDto;
import com.ysx.search.pojo.ApUserSearch;
import com.ysx.search.service.ApUserSearchService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* <p>
* APP用户搜索信息表 控制器</p>
* @author ljh
* @since 2021-10-09
*/
@Api(value="APP用户搜索信息表",tags = "ApUserSearchController")
@RestController
@RequestMapping("/apUserSearch")
public class ApUserSearchController extends AbstractCoreController<ApUserSearch> {

    private ApUserSearchService apUserSearchService;

    @Autowired
    private ApBehaviorEntryFeign apBehaviorEntryFeign;

    //注入
    @Autowired
    public ApUserSearchController(ApUserSearchService apUserSearchService) {
        super(apUserSearchService);
        this.apUserSearchService=apUserSearchService;
    }

    //分页查询搜索记录  每页查询 5条
    @PostMapping("/searchFive")
    public Result<PageInfo<ApUserSearch>> findByPage(@RequestBody SearchDto searchDto) {
        Integer size = searchDto.getSize();
        Integer page = searchDto.getPage();
        if (size == null || size > 5) {
            size = 5;
        }
        if (page == null || page <= 0) {
            page = 1;
        }


        String userInfo = RequestHeaderInfoUtils.getHeaderUserId();
        //设备
        ApBehaviorEntry entry = null;
        if ("0".equals(userInfo)) {
            entry = apBehaviorEntryFeign.findByUserIdOrEquipmentId(null, searchDto.getEquipmentId());

        } else {
            //登录的用户
            entry = apBehaviorEntryFeign.findByUserIdOrEquipmentId(Integer.parseInt(userInfo), null);
        }
        if (entry == null) {
            return Result.error();
        }
        IPage<ApUserSearch> pageObject = new Page<ApUserSearch>(page, size);
        QueryWrapper<ApUserSearch> queryWrapper = new QueryWrapper<ApUserSearch>();
        queryWrapper.eq("entry_id", entry.getId());
        //状态为1
        queryWrapper.eq("status", 1);
        IPage<ApUserSearch> page1 = apUserSearchService.page(pageObject, queryWrapper);
        PageInfo<ApUserSearch> pageInfo = getPageInfo(page1);
        return Result.ok(pageInfo);
    }

}

